package com.org.cys.service.impl.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.org.cys.dao.base.IGeneralDao;
import com.org.cys.pojo.base.Page;
import com.org.cys.pojo.system.data.PrivilegeEnum;
import com.org.cys.service.system.PrivilegeRoleService;
import com.org.cys.service.system.PrivilegeService;
import com.org.cys.util.IdWorker;
import com.org.cys.util.MapUtil;
import com.org.cys.util.PageData;
import com.org.cys.util.StringUtil;


/**
 *	t_system_privilege Service实现类
 *	@author caiyushen
 */
@Service("privilegeServiceImpl")
public class PrivilegeServiceImpl implements PrivilegeService {
  
  @Resource(name="generalDao")
  private IGeneralDao generalDao;
  
  @Resource(name = "privilegeRoleServiceImpl")
  private PrivilegeRoleService privilegeRoleService;
  
  public static String XMLMapper = "PrivilegeMapper";
  
  /**
	* 装载Privilege
	* @param PageData
	* @return
	* @throws Exception 
	* */
  @Override
  public PageData getEntityById(PageData pd) throws Exception {
	  return (PageData) generalDao.findForObject(XMLMapper+".findById", pd);
  }
  
  /**
	* 保存Privilege
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void save(PageData pd) throws Exception{
	  pd.put("id", IdWorker.getId());
	  pd.put("createDate", new Date());
	  pd.put("updateDate", new Date());
	  generalDao.save(XMLMapper+".save",pd);
  }
  
  /**
   * 批量保存Privilege
   * @param PageData
   * @throws Exception
   * */
  public void saveBatch(List<PageData> pdList) throws Exception{
	  Date nowDateTime = new Date();
	  for(PageData pd:pdList){
		  pd.put("updateDate", nowDateTime);
	  }
	  generalDao.batchSave(XMLMapper+".saveBatch",pdList);
  }
  
  /**
	* 更新Privilege
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void update(PageData pd) throws Exception{
	  pd.put("updateDate", new Date());
	  generalDao.update(XMLMapper+".update",pd);
  }
  
  /**
	* 删除Privilege
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void delete(PageData pd) throws Exception{
	  //先删除中间表数据
	  privilegeRoleService.deleteByPrivilegeId(MapUtil.getLong(pd, "id"));
	  //再删除Privilege
	  generalDao.delete(XMLMapper+".delete", pd);
  }
  
  /**
	* Privilege列表
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> dataList(Page page) throws Exception{
	  List<PageData> dataList = (List<PageData>) generalDao.findForList(XMLMapper+".dataListPage", page);
	  PrivilegeEnum.convert(dataList);
	  return dataList;
  }
  
  /**
	* 获取所有Privilege数据
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> listAll(PageData pd) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".listAll", pd);
  }
  
  /**
   * 获取所有父级Privilege数据
   * @param PageData
   * @throws Exception
   * */
  public List<PageData> getParentList(PageData pd) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".getParentList", pd);	 
  }
  
  /**
   * 根据父级id获取Privilege数据
   * @param parentId 父级id
   * @throws Exception
   * */
  public List<PageData> getListByParentId(Long parentId) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".getListByParentId", parentId);	 
  }
  
  /**
   * 根据父级id、权限类型获取Privilege数据
   * @param parentId 父级id
   * @param privilegeType 权限类型
   * @throws Exception
   * */
   public List<PageData> getListByParentIdAndType(Long parentId,Integer type) throws Exception{
	   List<PageData> dataList = new ArrayList<>();
	   if(StringUtil.checkObj(parentId)&&StringUtil.checkObj(type)){
		   PageData pd = new PageData();
		   pd.put("parentId", parentId);
		   pd.put("type", type);
		   dataList = (List<PageData>) generalDao.findForList(XMLMapper+".getListByParentIdAndType", pd);	 
	   }
	   return dataList;
   }
  
}